#pragma once
#include "creature.h"

/*羊，吃草，躲避狼*/
class Sheep : public Creature
{
public:
	Sheep();
	Sheep(int name);
	Sheep(int name, int x, int y, Flag flag);
	void choose_target_race(int mode);
	virtual void search();
};

Sheep::Sheep() {}

Sheep::Sheep(int name)
{
	m_flag = {2, 0, 1, 0, 0, 15, 100, 200, 1, 1};
	m_name = name;
	m_age = rand() % m_flag.age_up;												 /*年龄在上限内随机生成*/
	m_hp = m_flag.hp_up / 2 + (rand() % (m_flag.hp_up / 2 + 1));				 /*生命值在上限一半到上限内生成*/
	m_hunger = m_flag.hunger_full / 2 + (rand() % (m_flag.hunger_full / 2 + 1)); /*饥饿值在饱一半到全饱内生成*/
	m_flag.gender = 1 + (rand() % 2);
	m_position = {rand() % 50, rand() % 50};
}

Sheep::Sheep(int name, int x, int y, Flag flag)
{
	m_flag = flag; /*先把标签赋给新生物，确定种族和上限，其他的之后覆盖*/
	/*创建一个生物，随机生成数值,x和y坐标固定*/
	m_position.x = x;
	m_position.y = y;
	m_name = name;
	m_age = 1;													 /*新生物的年龄为1*/
	m_hp = m_flag.hp_up / 2 + (rand() % (m_flag.hp_up / 2 + 1)); /*生命值在上限一半到上限内生成*/
	m_hunger = m_flag.hunger_full / 4;							 /*饥饿值为饱/4，需低于繁衍线避免回交*/
	m_flag.gender = 1 + (rand() % 2);
}

void Sheep::choose_target_race(int mode)
{
	switch (mode)
	{
	case 1:
		m_target.race = nature.grass;
		m_target.num = &nature.num.grass;
		break;
	case 2:
		m_target.race = nature.sheep;
		m_target.num = &nature.num.sheep;
		break;
	default:
		break;
	}
}

void Sheep::search() /*羊在搜索时固定优先搜索有没有狼在视野里，如果有则将行动模式直接改为3*/
{
	int flag = 0;
	for (int i = 1; i <= nature.num.wolf; i++)
	{
		if (nature.wolf[i]->m_position.x <= m_position.x + m_sight || nature.wolf[i]->m_position.x >= m_position.x - m_sight)
		{
			if (nature.wolf[i]->m_position.y <= m_position.y + m_sight || nature.wolf[i]->m_position.y >= m_position.y - m_sight)
			{
				flag = 1;
				get_distance(*nature.wolf[i]);
				/*如果视野里搜索到狼，测定距离并选择对象*/
			}
		}
	}
	if (flag == 1)
	{
		m_flag.mode = 3;
		return;
	} /*确定有狼则准备逃命，直接退出函数*/
	for (int i = 1; i <= *m_target.num; i++)
	{
		Creature *c = m_target.race[i];
		judge(*c);
	}
	if (m_target.self == nullptr) /*如果搜索一圈后没有结果，则行动模式改为闲逛*/
	{
		m_flag.mode = 0;
	}
}
